<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Video;
use App\Models\Video as AppVideo;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use zgldh\QiniuStorage\QiniuStorage;

class VideoController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new AppVideo(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('video_cover')->image();
            $grid->column('title');
            $grid->column('recommend')->switch();
            $grid->column('status')->switch();
            $grid->column('created_at')->sortable();
            $grid->model()->orderBy('id', 'desc');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('title');
                $filter->equal('recommend')->select([0 => '不推荐', 1 => '推荐']);
                $filter->equal('status')->select([0 => '隐藏', 1 => '显示']);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new AppVideo(), function (Show $show) {

            $show->field('id');
            $show->field('video_cover')->image();
            $show->field('title');
            $show->field('keywords');
            $show->field('description');
            $show->field('content');
            $show->field('publish_time');
            $show->field('click');
            $show->field('status')->using(['0' => '隐藏', '1' => '显示'])->label();
            $show->field('author');
            $show->field('source');
            $show->field('recommend')->using(['0' => '不推荐', '1' => '推荐'])->label();
            $show->field('out_url');
            $show->field('video')->unescape()->as(function ($avatar) {

                return "<video src='{$avatar}' controls='controls' height='300' width='300'/>";
            });
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AppVideo(), function (Form $form) {
            $filename = md5(uniqid()) . '.mp4';
            $disk = QiniuStorage::disk('qiniu');
            // 回调信息, Dcat 上传文件需要的报文格式
            $callBack = [
                'status' => true,
                'data'   => [
                    'id'   => env('QINIU_DOMAINS') . '/' . $filename,
                    'name' => env('QINIU_DOMAINS') . '/' . $filename,
                    'path' => env('QINIU_DOMAINS') . '/' . $filename,
                    'url'  => env('QINIU_DOMAINS') . '/' . $filename,
                ],
            ];
            $policy   = [
                'returnBody' => json_encode($callBack),
            ];
            // 七牛云上传 token
            $token    = $disk->uploadToken($filename, 3600, $policy);


            $form->disableEditingCheck();
            $form->disableViewCheck();
            $form->hidden('recommend')
                ->customFormat(function ($v) {
                    return $v == 1 ? 1 : 0;
                })
                ->saving(function ($v) {
                    return $v;
                });

            $form->hidden('status')
                ->customFormat(function ($v) {
                    return $v == 1 ? 1 : 0;
                })
                ->saving(function ($v) {
                    return $v;
                });
            $form->display('id');
            $form->image('video_cover')->autoUpload()->move('video/video_cover/' . date('Y/m') . '/' . uniqid())->saveFullUrl();
            // $form->file('video') // 更改文件最大限制
            //     ->maxSize(512000)
            //     // 关闭页面删除
            //     ->removable(false)->accept('mp4')->help('仅支持MP4格式')->autoUpload()->move('video/video/' . date('Y/m') . '/' . uniqid())->saveFullUrl();
            $form->file('video')
                ->disk('qiniu')
                ->autoUpload()
                ->accept('mp4')
                // 提示文字
                ->help('仅支持MP4格式')
                ->maxSize(1024 * 1024)
                // 关闭页面删除
                ->removable(false)
                // 设置上传地址
                ->url('http://up-z1.qiniup.com')
                // 自定义 webuploader 配置
                // Dcat 默认 name 为 _file_
                ->options(['fileVal' => 'file'])
                // 上传事件
                ->on(
                    'startUpload',
                    <<<JS
        function () {
            console.log('文件开始上传...', this);
            // 上传文件前附加自定义参数到文件上传接口
            this.uploader.options.formData['token'] = "$token";
            this.uploader.options.formData['fileName'] ="$filename";
            this.uploader.options.formData['key'] = "$filename";
        }
JS
                )->on(
                    'uploadFinished',
                    <<<JS
        function () {
            console.log('文件上传完毕',this);
        }
JS
                );

            $form->text('title');
            $form->text('keywords');
            $form->textarea('description');

            $form->editor('content');
            $form->datetime('publish_time');
            $form->text('click');
            $form->radio('status')->options([0 => '隐藏', 1 => '显示'])->default(1);
            $form->text('sort', '排序')->help('排序越大越靠前')->value(0);

            $form->text('author');
            $form->text('source');

            $form->radio('recommend')->options([0 => '不推荐', 1 => '推荐'])->default(1);
            $form->text('out_url');


            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
